<?php
# 文件名称:site.model.php
# MetInfo在线反馈系统 
# Copyright (C) 长沙米拓信息技术有限公司 (http://www.metinfo.cn). All rights reserved.
class siteModel extends C_model {
	var $_questions   = array();
	
	var $_answers = array();
	
	var $_lang = null;
	
    function siteModel()
    {
        parent::C_model();
    }
	function getLanguage()
	{
		if (!empty( $this->_lang ))
		{
			return $this->_lang;
		}
		$SYS = &get_instance();
		//请求的语言
		$lang = $SYS->request->getVar( 'lang', '',	'get',	'cmd' );
		
		//是否存在该语言
		$query = 'SELECT *' .
				' FROM '.$this->_db->_table_prefix.'language'.' AS a '.
				' WHERE a.lang_type=1'.
				' AND a.enabled=1'.
				' AND a.lang_code="'.$lang.'"'
				;
		$this->_db->setQuery($query);
		$this->_lang = $this->_db->loadAssoc();
		if($this->_lang == null)
		{
			//前台已开启的语言
			$query = 'SELECT *' .
					' FROM '.$this->_db->_table_prefix.'language'.' AS a '.
					' WHERE a.lang_type=1'.
					' AND a.default=1'.
					' AND a.enabled=1'
					;
			$this->_db->setQuery($query);
			$defaultlang = $this->_db->loadAssoc();
			$lang = $SYS->request->getVar( 'lang', $defaultlang['lang_code'],'post','cmd' );
			//获取id
			$query = 'SELECT *' .
					' FROM '.$this->_db->_table_prefix.'language'.' AS a '.
					' WHERE a.lang_type=1'.
					' AND a.lang_code="'.$lang.'"'.
					' AND a.enabled=1'
					;
			$this->_db->setQuery($query);
			$this->_lang = $this->_db->loadAssoc();
		}		
		return $this->_lang;
	}
	function getForms()
	{
		
		//语言列
		$where[]='a.id=b.recordid';
		$where[]='a.enabled=1';
		$where[]='a.startdate<= Date(\''.date('Y-m-d',time()).'\')';
		$where[]='b.module=1';
		$where[]='b.fieldname="name"';
		$where[]='b.lang='.$this->_lang['id'];
		$where = (count($where) ? ' WHERE '.implode(' AND ', $where) : '');
		$orderby = ' ORDER BY  a.modified_date desc';
		//数据集
		$query = 'SELECT a.no_order,b.fieldvalue,a.created_by,a.enabled,a.modified_date,a.id' .
				' FROM '.$this->_db->_table_prefix.'fdform'.' AS a '.
				' ,'.$this->_db->_table_prefix.'fields'.' AS b '.
				$where.
				$orderby;
		$this->_forms = $this->_getArrayList( $query,0,20 );
		return $this->_forms;
	}
	function getForm()
	{
		if (!empty( $this->_form )) 
		{
			return $this->_form;
		}
		$SYS =&get_instance();
		$goUrl = WEBSELF.'/site/error';
		// 获取请求的数据
		$filter_form		= $SYS->request->getVar( "form",'','get','cmd' );
		if($filter_form == '') $filter_form = $SYS->request->getVar( "form",'','post','cmd');
		$SYS->request->setVar( "form",$filter_form);
		if($filter_form == '')
		{
			show_js_error(C_text::_('NotFind'),$goUrl);
			return;
		}
		$filter_form = str_replace(".html", "", $filter_form);
		//$decode = explode(":",base64_decode($filter_form));
		$filter_form = explode("-",$filter_form);
		
		if(count($filter_form) !=2)
		{
			show_js_error(C_text::_('NotFind'),$goUrl);
			return;
		}
		$query = 'SELECT a.*' .
				' FROM '.$this->_db->_table_prefix.'fdform'.' AS a '.
				' WHERE a.created_by='.$filter_form[0].
				' AND a.no_order='.$filter_form[1].
				' AND a.enabled=1'.
				' AND a.startdate<= Date(\''.date('Y-m-d',time()).'\')'.
				' ORDER BY a.no_order'
				;
		//加载第$filter_form条记录
		$this->_db->setQuery($query);
		$this->_form = $this->_db->loadAssoc();
		if($this->_form == null || !$this->permit($this->_form['access'],true))
		{
			show_js_error(C_text::_('NotFind'),$goUrl);
			return;
		}
		$this->_namefield 		= $this->getFieldName($this->_form['id'],"name");
		$this->_introducefield  = $this->getFieldName($this->_form['id'],"introduce");
		$this->_replysubject	= $this->getFieldName($this->_form['id'],"replysubject");
		$this->_replycontent    = $this->getFieldName($this->_form['id'],"replycontent");
		return $this->_form;
	}
	function getOnline()
	{
		$SYS =&get_instance();
		$access = unserialize($this->_form['params']);
		if(intval($access['online'])!=1)
			return array();
		//过滤查询
		$where = array();
		
		//语言列
		$where[]='a.id=b.recordid';
		$where[]='a.form='.$this->_form['id'];
		$where[]='b.module=2';
		$where[]='b.fieldname="name"';
		$where[]='b.lang='.$this->_lang['id'];		
		
		$orderby = ' ORDER BY a.no_order';
		$where = (count($where) ? ' WHERE '.implode(' AND ', $where) : '');
		
		//数据集
		$query = 'SELECT a.*,b.fieldvalue' .
				' FROM '.$this->_db->_table_prefix.'online'.' AS a '.
				' ,'.$this->_db->_table_prefix.'fields'.' AS b '.
				$where.
				$orderby;
		$tmp = $this->_getArrayList( $query );
		return $tmp;
	}
	function getFieldName($id,$name)
	{
		//语言
		$query = 'SELECT b.id,b.fieldvalue name'.
				' FROM '.$this->_db->_table_prefix.'fields'.' AS b '.
				' WHERE b.recordid='.$id.
				' AND b.module=1'.
				' AND b.lang='.$this->_lang['id'].
				' AND b.fieldname="'.$name.'"'			
		;
		$this->_db->setQuery($query);
		return $this->_db->loadAssoc();
	}
	function getName()
	{
		return $this->_namefield;
	}
	function getReplyContent()
	{
		return $this->_replycontent;
	}
	function getIntroduce()
	{
		return $this->_introducefield;
	}
	function getReplySubject()
	{
		return $this->_replysubject;
	}
	function getTitle()
	{		
		$query = 'SELECT b.fieldvalue name'.
				' FROM '.$this->_db->_table_prefix.'fields'.' AS b '.
				' WHERE b.recordid=0'.
				' AND b.module=0'.
				' AND b.lang='.$this->_lang['id'].
				' AND b.fieldname="sitename"'
		;
		$this->_db->setQuery($query);		
		$this->_titlefield = $this->_db->loadAssoc();
		return $this->_titlefield;
	}
	function permit($access)
	{
		$SYS =&get_instance();
		$db = &$this->_db;
		//游客
		$db->setQuery(
			'SELECT *' .
			' FROM '.$db->_table_prefix.'usergroups AS a'.
			' RIGHT JOIN '.$db->_table_prefix.'usergroups AS b'.
			' ON b.parent_id=a.id'.
			' WHERE a.id='.$access
		);
		if($db->loadResult() == null)
		{
			return true;
		}
		//当前表单权限
		$db->setQuery(
			'SELECT a.lft,a.rgt' .
			' FROM '.$db->_table_prefix.'usergroups AS a'.
			' WHERE a.id='.$access
		);
		$g1 = $db->loadObject();
		//当前用户
		$me	 = $SYS->session->get('admin_name');
		if($me == null)
		{
			return false;
		}else
		{
			$db->setQuery(
				'SELECT a.lft,a.rgt' .
				' FROM '.$db->_table_prefix.'usergroups AS a'.
				' WHERE a.id='.$me->get('gid')
			);
			$g2 = $db->loadObject();
		}		
		if($g1 && $g2 && $g1->lft >= $g2->lft && $g1->rgt <= $g2->rgt)
			return true;
		else
			return false;
	}
	function getQuestions()
	{		
		$SYS =&get_instance();
		// 获取请求的数据
		$form = $this->getForm();
		//过滤查询
		$orderby = ' ORDER BY a.no_order';
		//分页
		//题目
		$query = 'SELECT a.*,b.fieldvalue name' .
				' FROM '.$this->_db->_table_prefix.'fdparameter'.' AS a '.
				' LEFT JOIN '.$this->_db->_table_prefix.'fields AS b ON ( b.module =3 AND b.recordid = a.id AND b.lang='.$this->_lang['id'].')'.
				' WHERE a.form='.$form['id'].
				' AND use_ok=1'.
				$orderby;		
		$this->_questions = $this->_getArrayList( $query);
		foreach($this->_questions AS $key => $val)
		{
			if(!$this->permit($val['access']))
			{
				unset($this->_questions[$key]);
			}
		}
		return $this->_questions;
	}
	function getAnswers()
	{
		$SYS =&get_instance();
		// 获取请求的数据
		$form = $this->getForm();
		//过滤查询
		$orderby = ' ORDER BY a.no_order';
		//问题答案
		$query = ' SELECT id' .
				' FROM '.$this->_db->_table_prefix.'fdparameter'.' AS b '.
				' WHERE b.form='.$form['id'].
				' AND use_ok=1'
				;
		$this->_db->setQuery($query);
		$tmplist = $this->_db->loadResultArray();
		$query = 'SELECT a.*,d.fieldvalue name' .
				' FROM '.$this->_db->_table_prefix.'fdlist'.' AS a '.
				' LEFT JOIN '.$this->_db->_table_prefix.'fields AS d ON ( d.module =4 AND d.recordid = a.id AND d.lang='.$this->_lang['id'].' )'.
				' WHERE a.bigid'.
				' IN ('.implode(",", $tmplist).' )'.
				$orderby;		
		$this->_answers = $this->_getObjectList($query);
		return $this->_answers;
	}
}
# 本程序是一个开源系统,使用时请你仔细阅读使用协议,商业用途请自觉购买商业授权.
# Copyright (C) 长沙米拓信息技术有限公司 (http://www.metinfo.cn). All rights reserved.
?>